﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Novel.Server.Response;

namespace Novel.Server.Middleware
{
    public static class ValidationResponseFactory
    {
        public static IActionResult CreateErrorResponse(ActionContext context)
        {
            var errorMessages = context.ModelState
                .Where(e => e.Value.Errors.Any())
                .Select(e => $"{e.Key}: {string.Join(", ", e.Value.Errors.Select(err => err.ErrorMessage))}")
                .ToArray();

            var errorMessage = string.Join("; ", errorMessages);
            var response = ApiResponse.ErrorResponse(errorMessage, 400);
            return new ObjectResult(response)
            {
                StatusCode = 400
            };
        }
    }
}

